import 'package:flutter/material.dart';
import 'package:ninetyfour/l10n/app_localizations.dart';
import '../../models/adress_info_model.dart';

class PayAddressView extends StatefulWidget {
  final List<AdressInfo> adressInfo;
  final VoidCallback onCLickAdd;
  final ValueChanged<int> onChangeAddress;
  const PayAddressView({super.key, required this.adressInfo, required this.onCLickAdd, required this.onChangeAddress});

  @override
  _PayAddressViewState createState() => _PayAddressViewState();
}

class _PayAddressViewState extends State<PayAddressView> {
  // 定义选中的地址索引，默认为0
  int selectIndex = 0;

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.only(left: 15, right: 15, top: 15),
      child: Container(
        decoration: BoxDecoration(
          color: const Color(0xFFEAEAFF),
          borderRadius: BorderRadius.circular(15),
        ),
        child: Column(
          children: [
            Padding(
              padding: const EdgeInsets.only(left: 15,top: 12,bottom: 12),
              child: Row(
                children: [
                  SizedBox(
                    width: 20,
                    height: 20,
                    child: Image.asset('assets/images/账单.png'),
                  ),
                  const SizedBox(width: 7),
                  Text(
                    AppLocalizations.of(context)!.zhangdandizhi,
                    style: const TextStyle(fontSize: 15, color: Colors.black),
                  ),
                  const Spacer(),
                  if (widget.adressInfo.isNotEmpty)            
                  TextButton.icon(
                    icon: SizedBox(
                      width: 20,
                      height: 20,
                      child: Image.asset('assets/images/下载.png'),
                    ),
                    label: Text(AppLocalizations.of(context)!.newcreateAddress),
                    style: TextButton.styleFrom(
                      foregroundColor: const Color(0xFF5D5FEE),
                      textStyle: const TextStyle(fontSize: 15),
                      padding: const EdgeInsets.only(right: 15,top: 0,bottom: 0),
                      minimumSize: Size.zero,
                      tapTargetSize: MaterialTapTargetSize.shrinkWrap
                    ),
                    onPressed: () {
                      widget.onCLickAdd();
                    },
                  ),
                ],
              ),
            ),
            // 使用 shrinkWrap 来使 ListView 根据内容自适应高度
            ClipRRect(
              borderRadius: BorderRadius.circular(15),
              child: Container(
                color: Colors.white, // 设置背景色为白色
                child: widget.adressInfo.isEmpty
                    ? Center(
                        child: Container(
                          color: Colors.white,
                          child: TextButton.icon(
                            icon: SizedBox(
                              width: 28,
                              height: 28,
                              child: Image.asset('assets/images/下载.png'),
                            ),
                            label: Text(AppLocalizations.of(context)!.newcreateAddress),
                            style: TextButton.styleFrom(
                              foregroundColor: const Color(0xFF5D5FEE),
                              textStyle: const TextStyle(fontSize: 17),
                              padding: const EdgeInsets.only(right: 17),
                            ),
                            onPressed: () {
                              widget.onCLickAdd();
                            },
                          ),
                        ),
                      )
                    : ListView.builder(
                        padding: EdgeInsets.zero,
                        shrinkWrap: true,
                        physics: NeverScrollableScrollPhysics(),
                        itemCount: widget.adressInfo.length,
                        itemBuilder: (context, index) {
                          final model = widget.adressInfo[index];
                          return Column(
                            children: [
                              GestureDetector(
                                onTap: () {
                                  setState(() {
                                    selectIndex = index;
                                  });
                                },
                                child: AdressCell(
                                  isSelected: selectIndex == index,
                                  model: model,
                                  onSetBtnTapped: () {
                                    widget.onChangeAddress(index);
                                  },
                                ),
                              ),
                              if (index != widget.adressInfo.length - 1)
                                Padding(
                                  padding: const EdgeInsets.symmetric(horizontal: 15),
                                  child: Container(
                                    height: 1,
                                    color: Color(0xFFF6F6F6),
                                  ),
                                ),
                            ],
                          );
                        },
                      ),
              ),
            )
          ],
        ),
      ),
    );
  }
}

class AdressCell extends StatelessWidget {
  final bool isSelected;
  final AdressInfo model;
  final VoidCallback onSetBtnTapped;

  const AdressCell({
    super.key,
    required this.isSelected,
    required this.model,
    required this.onSetBtnTapped,
  });

@override
Widget build(BuildContext context) {
  return Column(
    children: [
      Container(
        color: Colors.white,
        padding: const EdgeInsets.symmetric(vertical: 17, horizontal: 15),
        child: Row(
          children: [
            Image.asset(
              isSelected ? 'assets/images/address1.png' : 'assets/images/address0.png',
              width: 18,
              height: 18,
            ),
            const SizedBox(width: 10),
            // 用 Expanded 限制最大可用空间
            Expanded(
              child: Text(
                '${model.firstName} ${model.lastName} ${model.phoneNumber}',
                maxLines: 1,
                overflow: TextOverflow.ellipsis, // 超出部分显示省略号
                style: TextStyle(
                  fontSize: 14,
                  color: isSelected ? Colors.blue : const Color(0xFF333333),
                ),
              ),
            ),
            const SizedBox(width: 10),
            GestureDetector(
              onTap: onSetBtnTapped,
              child: Image.asset(
                'assets/images/setbtn.png',
                width: 18,
                height: 18,
              ),
            ),
          ],
        ),
      ),
    ],
  );
}

}
